package com.sitech.portal.server.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.portal.server.dao.ICalendarDao;
import com.sitech.portal.util.CommUtil;

public class CalendarDaoImpl extends BaseIbnmsDao implements ICalendarDao {

	
    /**
     * 查询用户所有的任务计划
     * @param LOGIN_ID
     * @return 
     */
    public List findCalendarEvents(String LOGIN_ID) {
        return this.query("findCalendarEvents", LOGIN_ID);
    }

    /**
     * 修改任务计划
     * @param event
     * @param LOGIN_ID 
     */
    public void updateCalendarEvent(Map event, String LOGIN_ID) throws Exception{
        if (event.get("id") == null || "".equals(event.get("id"))) {
            throw new Exception("在 updateCalendarEvent 时， event 的 id 不能为空！");
        }
        HashMap cal = new HashMap();
        for (int i = 0; i < CommUtil.CALENDAR_COLUMNS.size(); i++) {
            String value = event.get(CommUtil.EVENT_PARAMS.get(i)) + "";
            if (value == null || "null".equals(value) || "undefined".equals(value)) {
                value = "";
            }
            cal.put(CommUtil.CALENDAR_COLUMNS.get(i), value);
        }
        cal.put("LOGIN_ID", LOGIN_ID);
        this.insert("updateCalendarEvent", cal);
    }

    /**
     * 新增任务计划，返回新任务计划的id
     * @param event
     * @param LOGIN_ID
     * @return 
     */
    public String insertCalendarEvent(Map event, String LOGIN_ID) {
        HashMap cal = new HashMap();
        for (int i = 0; i < CommUtil.CALENDAR_COLUMNS.size(); i++) {
            String value = event.get(CommUtil.EVENT_PARAMS.get(i)) + "";
            if (value == null || "null".equals(value) || "undefined".equals(value)) {
                value = "";
            }
            cal.put(CommUtil.CALENDAR_COLUMNS.get(i), value);
        }
        cal.put("LOGIN_ID", LOGIN_ID);
        //从序列获取新的id
        String id = (String) this.querySingle("findCalendarEventID");
        cal.put("ID", id);
        this.insert("insertCalendarEvent", cal);
        return id;
    }

    public void deleteCalendarEvent(String id, String LOGIN_ID) {
        HashMap param = new HashMap();
        param.put("ID", id);
        param.put("LOGIN_ID", LOGIN_ID);
        this.delete("deleteCalendarEvent", param);
    }

    /**
     * 查找start_time在最近一段时间的Calendar事件，单位秒
     * @param seconds 时间段
     * @return 
     */
    public List findCalendarEventInRecentSeconds(Long seconds) {
        return this.query("findCalendarEventBefore", seconds);
    }
}
